import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
from random import randint
from os import system
from sys import stdin


def fontlen(string: str) -> float:
    return 1.2 * len([a for a in string if ord(a) <= 255]) + 2 * len(
        [a for a in string if ord(a) > 255])


class Indicator:

    def __init__(self, info: list[str]) -> None:
        height = 200
        width = 1920
        self.canvas = np.zeros((height, width, 4), dtype=np.uint8)
        self.names = info[0:3]  # 行政区划
        self.location = info[-1]  # 地理位置
        self.BusLineTitle = info[3]  # 线路标题，如 南京公交
        self.BusLineName = info[4]  # 线路名字
        self.BusTakeTime = info[5]  # 乘车时间
        self.BusPrice = info[6]  # 票价
        self.BusModel = info[7]  # 车型
        self.BusStations = info[8:12]  # 始发/上车/下车/终到
        if self.names[1] == '*':
            self.names[1] = ''

    def __DrawChinese(self,
                      word: str,
                      fontsize: int,
                      color: tuple[int, int, int, int],
                      pos: tuple[int, int],
                      font: str = 'msyh.ttc') -> None:
        image_pil = Image.fromarray(self.canvas)
        draw = ImageDraw.Draw(image_pil)
        draw.text(xy=pos,
                  text=word,
                  font=ImageFont.truetype(font, fontsize),
                  fill=color)
        self.canvas = np.array(image_pil)

    def __TitleDraw(self):
        # 绘制左侧正方形
        RGBorange = (0, 159, 255, 255)
        RGBwhite = (255, 255, 255, 160)
        square_size = (200, 200)
        square_position0 = (0, 0)
        square_position1 = (10, 10)
        square_position2 = (20, 20)
        cv2.rectangle(self.canvas,
                      square_position0, (square_size[0] - square_position0[0],
                                         square_size[1] - square_position0[1]),
                      RGBorange,
                      thickness=-1)
        cv2.rectangle(self.canvas,
                      square_position1, (square_size[0] - square_position1[0],
                                         square_size[1] - square_position1[1]),
                      RGBwhite,
                      thickness=-1)
        cv2.rectangle(self.canvas,
                      square_position2, (square_size[0] - square_position2[0],
                                         square_size[1] - square_position2[1]),
                      RGBorange,
                      thickness=-1)
        self.__DrawChinese(self.BusLineTitle[0:2], 75, (255, 255, 255, 255),
                           (25, 10))
        self.__DrawChinese(self.BusLineTitle[2:4], 75, (255, 255, 255, 255),
                           (25, 85))

    def __LocationDraw(self):
        RGBorange1 = (120 + randint(0, 15), 0, 255, 255)
        RGBorange2 = (0, 167 + randint(0, 15), 240, 255)
        RGBorange3 = (0, 135 + randint(0, 15), 240, 255)
        RGBorange4 = (0, 103 + randint(0, 15), 240, 255)
        RGBdeepblue = (0x80 + randint(0, 15), 0x66 + randint(0, 15),
                       0x33 + randint(0, 15), 255)
        RGBwhite = (255, 255, 255, 255)
        edge_size = 10
        font_size = 50
        font_size2 = font_size // 2
        total_size = font_size + edge_size * 2
        total_size2 = total_size // 2
        begin1 = 201
        begin2 = begin1 + edge_size * 2 + font_size2 * 0.9 * fontlen(
            self.BusTakeTime)
        begin3 = begin2 + edge_size * 2 + font_size2 * fontlen(
            self.names[0]) + total_size2
        begin4 = begin3 + edge_size * 2 + font_size2 * fontlen(
            self.names[1]) + total_size2
        begin5 = begin4 + edge_size * 2 + font_size2 * fontlen(
            self.names[2]) + total_size2
        begin6 = 1920 - total_size * 5 // 2
        scale1 = np.array(
            [[[begin1, 0], [begin2, 0], [begin2 + total_size2, total_size2],
              [begin2, total_size], [begin1, total_size]]],
            dtype=np.int32)
        scale2 = np.array(
            [[[begin2, 0], [begin3, 0], [begin3 + total_size2, total_size2],
              [begin3, total_size], [begin2, total_size],
              [begin2 + total_size2, total_size2]]],
            dtype=np.int32)
        scale3 = np.array(
            [[[begin3, 0], [begin4, 0], [begin4 + total_size2, total_size2],
              [begin4, total_size], [begin3, total_size],
              [begin3 + total_size2, total_size2]]],
            dtype=np.int32)
        scale4 = np.array(
            [[[begin4, 0], [begin5, 0], [begin5 + total_size2, total_size2],
              [begin5, total_size], [begin4, total_size],
              [begin4 + total_size2, total_size2]]],
            dtype=np.int32)
        scale5 = np.array(
            [[[begin5, 0], [begin6, 0], [begin6 + total_size, total_size],
              [begin5, total_size], [begin5 + total_size2, total_size2]]],
            dtype=np.int32)
        scale6 = np.array(
            [[[begin6 + total_size2, 0], [1920, 0], [1920, total_size],
              [begin6 + total_size2 + total_size, total_size]]],
            dtype=np.int32)
        cv2.fillPoly(self.canvas, scale1, RGBorange1)
        cv2.fillPoly(self.canvas, scale2, RGBorange2)
        cv2.fillPoly(self.canvas, scale3, RGBorange3)
        cv2.fillPoly(self.canvas, scale4, RGBorange4)
        cv2.fillPoly(self.canvas, scale5, RGBdeepblue)
        cv2.fillPoly(self.canvas, scale6, RGBdeepblue)
        self.__DrawChinese(self.BusTakeTime, font_size, RGBwhite,
                           (begin1 + edge_size, edge_size + 5), 'consola.ttf')
        self.__DrawChinese(self.names[0], font_size, RGBwhite,
                           (begin2 + edge_size + total_size2, edge_size - 10))
        self.__DrawChinese(self.names[1], font_size, RGBwhite,
                           (begin3 + edge_size + total_size2, edge_size - 10))
        self.__DrawChinese(self.names[2], font_size, RGBwhite,
                           (begin4 + edge_size + total_size2, edge_size - 10))
        self.__DrawChinese(self.location, font_size, RGBwhite,
                           (begin5 + edge_size + total_size2, edge_size - 10))

    def __StationDraw(self):
        edge_size = 10
        font_size = 50
        font_size2 = font_size // 2
        total_size = font_size + edge_size * 2
        total_size2 = total_size // 2
        RGBBlue1 = (150 + randint(-10, 10), 120 + randint(-10, 10),
                    90 + randint(-10, 10), 255)
        RGBBlue2 = (200 + randint(-10, 10), 180 + randint(-10, 10),
                    120 + randint(-10, 10), 255)
        RGBBlue3 = (120 + randint(-10, 10), 0 + randint(-10, 10),
                    120 + randint(-10, 10), 255)
        RGBBlue4 = (200 + randint(-10, 10), 180 + randint(-10, 10),
                    120 + randint(-10, 10), 255)
        RGBBlue5 = (150 + randint(-10, 10), 120 + randint(-10, 10),
                    90 + randint(-10, 10), 255)
        RGBwhite = (255, 255, 255, 255)
        start_station = self.BusStations[0]
        if start_station == '*':
            start_station = "始发上"
            RGBBlue1 = (80, 80, 80, 255)
        end_station = self.BusStations[3]
        if end_station == '*':
            end_station = "终到下"
            RGBBlue5 = (80, 80, 80, 255)
        begin1 = 201
        begin2 = begin1 + edge_size * 2 + font_size2 * fontlen(start_station)
        begin3 = begin2 + edge_size * 2 + font_size2 * fontlen(
            self.BusStations[1]) + total_size2
        begin4 = begin3 + edge_size * 2 + font_size2 * fontlen(
            self.BusLineName) + total_size2
        begin5 = begin4 + edge_size * 2 + font_size2 * fontlen(
            self.BusStations[2]) + total_size2
        begin6 = begin5 + edge_size * 2 + font_size2 * fontlen(
            end_station) + total_size2
        scale1 = np.array(
            [[[begin1, total_size], [begin2, total_size],
              [begin2 + total_size2, total_size2 * 3],
              [begin2, total_size * 2], [begin1, total_size * 2]]],
            dtype=np.int32)
        scale2 = np.array([[[begin2, total_size], [begin3, total_size],
                            [begin3 + total_size2, total_size2 * 3],
                            [begin3, total_size * 2], [begin2, total_size * 2],
                            [begin2 + total_size2, total_size2 * 3]]],
                          dtype=np.int32)
        scale3 = np.array([[[begin3, total_size], [begin4, total_size],
                            [begin4 + total_size2, total_size2 * 3],
                            [begin4, total_size * 2], [begin3, total_size * 2],
                            [begin3 + total_size2, total_size2 * 3]]],
                          dtype=np.int32)
        scale4 = np.array([[[begin4, total_size], [begin5, total_size],
                            [begin5 + total_size2, total_size2 * 3],
                            [begin5, total_size * 2], [begin4, total_size * 2],
                            [begin4 + total_size2, total_size2 * 3]]],
                          dtype=np.int32)
        scale5 = np.array([[[begin5, total_size], [begin6, total_size],
                            [begin6 + total_size2, total_size2 * 3],
                            [begin6, total_size * 2], [begin5, total_size * 2],
                            [begin5 + total_size2, total_size2 * 3]]],
                          dtype=np.int32)
        cv2.fillPoly(self.canvas, scale1, RGBBlue1)
        cv2.fillPoly(self.canvas, scale2, RGBBlue2)
        cv2.fillPoly(self.canvas, scale3, RGBBlue3)
        cv2.fillPoly(self.canvas, scale4, RGBBlue4)
        cv2.fillPoly(self.canvas, scale5, RGBBlue5)
        self.__DrawChinese(start_station, font_size, RGBwhite,
                           (begin1 + edge_size, total_size + edge_size - 10))
        self.__DrawChinese(
            self.BusStations[1], font_size, RGBwhite,
            (begin2 + edge_size + total_size2, total_size + edge_size - 10))
        self.__DrawChinese(
            self.BusLineName, font_size, RGBwhite,
            (begin3 + edge_size + total_size2, total_size + edge_size - 10))
        self.__DrawChinese(
            self.BusStations[2], font_size, RGBwhite,
            (begin4 + edge_size + total_size2, total_size + edge_size - 10))
        self.__DrawChinese(
            end_station, font_size, RGBwhite,
            (begin5 + edge_size + total_size2, total_size + edge_size - 10))

    def __PriceDraw(self):
        edge_size = 10
        font_size = 50
        font_size2 = font_size // 2
        total_size = font_size + edge_size * 2
        total_size2 = total_size // 2
        begin1 = 1920
        RGBwhite = (255, 255, 255, 255)
        begin2 = begin1 - font_size2 * fontlen(self.BusModel) - edge_size * 2
        begin3 = begin2 - font_size2 * fontlen(
            self.BusPrice) - edge_size * 2 - total_size2
        scale1 = np.array(
            [[[begin1, 130], [begin1, 130 + total_size],
              [begin2, 130 + total_size],
              [begin2 - total_size2, 130 + total_size2], [begin2, 130]]],
            dtype=np.int32)
        scale2 = np.array(
            [[[begin2, 130], [begin2 - total_size2, 130 + total_size2],
              [begin2, 130 + total_size],
              [begin3 - total_size, 130 + total_size], [begin3, 130]]],
            dtype=np.int32)
        cv2.fillPoly(self.canvas, scale1, (00, 80, 00, 255))
        cv2.fillPoly(self.canvas, scale2, (00, 160, 80, 255))
        self.__DrawChinese(self.BusPrice, font_size, RGBwhite,
                           (begin3 + edge_size, 130 + edge_size - 10))
        self.__DrawChinese(self.BusModel, font_size, RGBwhite,
                           (begin2 + edge_size, 130 + edge_size - 10))

    def Draw(self, filename: str):
        self.__TitleDraw()
        self.__LocationDraw()
        self.__StationDraw()
        self.__PriceDraw()
        cv2.imwrite(filename, self.canvas)


system("mkdir -p image")
index = 0
for info in stdin.readlines():
    info = info.strip()
    if len(info) != 0:
        info = info.split()
        head = info[0:12]
        last = info[12:]
        for last_ in last:
            obj = Indicator(head + [last_])
            obj.Draw(f"image/{index}.png")
            index += 1
